package product;

import java.awt.EventQueue;
import javax.swing.ButtonGroup;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JLabel;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import javax.swing.JTable;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.UIManager;
import javax.swing.JRadioButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPanel;

import java.awt.BorderLayout;
import java.awt.Font;

public class CustomerList extends JInternalFrame {
	
	// 버튼을 그룹해줌
	ButtonGroup bg = new ButtonGroup();

	DefaultTableModel model, model2;
	
	//테이블
	Vector<String> v = new Vector<String>();
	Vector<String> v2 = new Vector<String>();

	Map<String, CompanyData> customerMap;
	Map<Long, DeliveryData> itemMap;
		
	DeliveryUpdate du;
	CompanyUpdate cu;
	
	private JTextField textserch;
	private JButton btnNewButton;
	private JScrollPane scrollPane;
	private JScrollPane scrollPane2;
	private JLabel label;
	JTable table;
	JTable table2;
	private JRadioButton rdbtnNewRadioButton;
	private JRadioButton rdbtnNewRadioButton_1;
	private JLabel lblNewLabel;
	private JPanel panel;
	private JPanel panel2;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					CustomerList frame = new CustomerList();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	//customerMap과 itemMap, CompanyUpdate와 DeliveryUedate의 정보를 가져오기위하여
	public void setD(Map cus, HashMap item, CompanyUpdate cu, DeliveryUpdate du) {
		this.customerMap = cus;
		this.itemMap = item;
		this.cu = cu;
		this.du = du;
		
	}

	public CustomerList() {
		super("CustomerList", true, true, true, false);

		setBounds(100, 100, 545, 568);
		getContentPane().setLayout(null);
		getContentPane().add(getTextserch());
		getContentPane().add(getBtnNewButton());
		bg.add(getRdbtnNewRadioButton());
		bg.add(getRdbtnNewRadioButton_1());

		v.add("회사명");
		v.add("사업자번호");
		v.add("주소");
		v.add("전화번호");
		v.add("fax");
		v.add("대표자");

		model = new DefaultTableModel(v, 0) {
			@Override
			public boolean isCellEditable(int row, int column) { // 테이블에서 수정
																	// 안되게함~
				return false;
			}
		};
		v2.add("제품번호");
		v2.add("제품명");
		v2.add("단가");
		v2.add("수량");
		v2.add("사업자번호");
		v2.add("회사명");
		v2.add("대표자명");
		
		model2 = new DefaultTableModel(v2, 0) {
			@Override
			public boolean isCellEditable(int row, int column) { // 테이블에서 수정
																	// 안되게함~
				return false;
			}
		};
		
		getContentPane().add(getRdbtnNewRadioButton());
		getContentPane().add(getRdbtnNewRadioButton_1());
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getLabel());
		getContentPane().add(getPanel());
	}

	
	//customer의 내용을 테이블에 출력하기 위한 메소드
	public void customerPrint() {
		Set<String> st = customerMap.keySet();
		Iterator<String> it = st.iterator();

		while (it.hasNext()) {
			String key = it.next();
			CompanyData val = customerMap.get(key);

			if (val.getName().contains(textserch.getText())) {
				Vector<String> vv = new Vector<String>();

				vv.add(val.getName());
				vv.add(val.getCode());
				vv.add(val.getAddress());
				vv.add(val.getPhone());
				vv.add(val.getFax());
				vv.add(val.getCeo());

				model.addRow(vv);

			} else {
				JOptionPane.showMessageDialog(null, "자료가 없습니다.");

			}
		}

	}
	//delivery를 테이블에 출력하기 위한 메소드
	public void deliveryPrint() {
		
		Set<Long> st = itemMap.keySet();
		Iterator<Long> it = st.iterator();

		while (it.hasNext()) {
			Long key = it.next();
			DeliveryData val = itemMap.get(key);

			if (val.getItemName().contains(textserch.getText())) {
				
				Vector<String> vv = new Vector<String>();

				vv.add(val.getItemNumber()+"");
				vv.add(val.getItemName());
				vv.add(val.getPrice());
				vv.add(val.getCount());
				vv.add(val.getCode());
				vv.add(val.getName());
				vv.add(val.getCeo());

				model2.addRow(vv);

			} else {
				JOptionPane.showMessageDialog(null, "자료가 없습니다.");

			}
		}
	}

	//Customer 테이블을 클릭했을시 이벤트
	private class MyMouseListener extends MouseAdapter {

		@Override
		public void mouseClicked(MouseEvent e) {
			if (e.getButton() == 1) { // 원 클릭

				Vector<String> ve = new Vector<String>();

				//선택된 행의 인덱스를 구함
				int q = table.getSelectedRow();

				//(행,열)에 있는 정보를 벡터에 저장
				ve.add((String) table.getValueAt(q, 0));
				ve.add((String) table.getValueAt(q, 1));
				ve.add((String) table.getValueAt(q, 2));
				ve.add((String) table.getValueAt(q, 3));
				ve.add((String) table.getValueAt(q, 4));
				ve.add((String) table.getValueAt(q, 5));

				
				String f=(ve.get(0));
				String a =(ve.get(2));
				String b =(ve.get(3));
				String c =(ve.get(4));
				String d =(ve.get(5));

				cu.setVisible(true);
				cu.setBounds(650, 100, 894, 589);
				getTable_1().editingStopped(null);
			}
		}
	}
	
	//Delivery 테이블을 클릭했을시 이벤트
	private class MyMouseListener2 extends MouseAdapter {

		@Override
		public void mouseClicked(MouseEvent e) {
			if (e.getButton() == 1) { // 원 클릭
				
				Vector<String> ve = new Vector<String>();

				int q = table2.getSelectedRow();

				ve.add((String) table2.getValueAt(q, 0));
				ve.add((String) table2.getValueAt(q, 1));
				ve.add((String) table2.getValueAt(q, 2));
				ve.add((String) table2.getValueAt(q, 3));
				ve.add((String) table2.getValueAt(q, 4));
				ve.add((String) table2.getValueAt(q, 5));
				ve.add((String) table2.getValueAt(q, 6));

				long key = Long.parseLong(ve.get(0));
				du.index=key;

				du.inputItemName.setText(ve.get(1));
				du.inputPrice.setText(ve.get(2));
				du.inputEa.setValue(Integer.parseInt(ve.get(3)));
				du.inputCode.setText(ve.get(4));
				du.inputName.setText(ve.get(5));
				du.inputCEO.setText(ve.get(6));
				
				du.table0(table2, model2,q);
				du.setVisible(true);
				du.setBounds(650, 100, 450, 288);

			}
		}
	}

	private JTextField getTextserch() {
		if (textserch == null) {
			textserch = new JTextField();
			textserch.setBounds(137, 71, 165, 23);
			textserch.setColumns(10);
		}
		return textserch;
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("검색");
			btnNewButton.setFont(new Font("돋움", Font.PLAIN, 13));
			btnNewButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {

					// 선택 여부 반환 (선택:true //아니면 false)
					// 거래처가 체크되어 있으면
					boolean customer = getRdbtnNewRadioButton().isSelected();
					// 품목이 체크되어 있으면
					boolean item = getRdbtnNewRadioButton_1().isSelected();

					if (customer == true) {
						model.setNumRows(0); // 모델데이터 초기화  (setRowCount(0))
						customerPrint();
					} else if (item == true) {
						model2.setNumRows(0); // 모델데이터 초기화
						deliveryPrint();
					} else if (customer == false && item == false) {
						JOptionPane.showMessageDialog(null, "거래처 or 품목을 골라주세요");
					}
				}
			});
			btnNewButton.setBackground(UIManager.getColor("CheckBox.light"));
			btnNewButton.setBounds(314, 71, 97, 23);
		}
		return btnNewButton;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTable_1());
		}
		return scrollPane;
	}
	
	private JScrollPane getScrollPane2(){
		if (scrollPane2 == null) {
			scrollPane2 = new JScrollPane();
			scrollPane2.setViewportView(getTable_2());
		}
		return scrollPane2;
	}

	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("");
			label.setBorder(new TitledBorder("검 색 방 법"));
			label.setBackground(UIManager.getColor("Button.background"));
			label.setOpaque(true);
			label.setBounds(10, 10, 507, 100);
		}
		return label;
	}

	public JTable getTable_1() {
		if (table == null) {
			table = new JTable(model);
			
			table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);    //테이블 각 셀의 너비 설정~
			table.getColumnModel().getColumn(0).setPreferredWidth(50);
			table.getColumnModel().getColumn(1).setPreferredWidth(65);
			table.getColumnModel().getColumn(2).setPreferredWidth(110);
			table.getColumnModel().getColumn(3).setPreferredWidth(110);
			table.getColumnModel().getColumn(4).setPreferredWidth(110);

			table.addMouseListener(new MyMouseListener());
		}
		return table;
	}
	
	private JTable getTable_2() {
		if (table2 == null) {
			table2 = new JTable(model2);
			
			table2.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);    //테이블 각 셀의 너비 설정~
			table2.getColumnModel().getColumn(0).setPreferredWidth(60);
			table2.getColumnModel().getColumn(1).setPreferredWidth(65);
			table2.getColumnModel().getColumn(2).setPreferredWidth(110);
			table2.getColumnModel().getColumn(3).setPreferredWidth(90);
			table2.getColumnModel().getColumn(4).setPreferredWidth(70);

			table2.addMouseListener(new MyMouseListener2());
		}
		return table2;
	}

	private JRadioButton getRdbtnNewRadioButton() {
		if (rdbtnNewRadioButton == null) {
			rdbtnNewRadioButton = new JRadioButton("거래처");
			rdbtnNewRadioButton.setFont(new Font("돋움", Font.PLAIN, 13));
			rdbtnNewRadioButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					lblNewLabel.setText("           회사명 : ");
					getContentPane().remove(getPanel2()); //Panel2를지움
					getContentPane().add(getPanel());	  //Panel을 붙임
				}
			});

			rdbtnNewRadioButton.setBackground(UIManager
					.getColor("CheckBox.light"));
			rdbtnNewRadioButton.setBounds(137, 42, 68, 23);
		}
		return rdbtnNewRadioButton;
	}

	private JRadioButton getRdbtnNewRadioButton_1() {
		if (rdbtnNewRadioButton_1 == null) {
			rdbtnNewRadioButton_1 = new JRadioButton("품목");
			rdbtnNewRadioButton_1.setFont(new Font("돋움", Font.PLAIN, 13));

			rdbtnNewRadioButton_1.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					lblNewLabel.setText("               품목 : ");
					getContentPane().remove(getPanel()); //Panel을 지움
					getContentPane().add(getPanel2());	 //Panel2을 붙임

				}
			});

			rdbtnNewRadioButton_1.setBackground(UIManager
					.getColor("CheckBox.light"));
			rdbtnNewRadioButton_1.setBounds(214, 42, 121, 23);
		}
		return rdbtnNewRadioButton_1;
	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("거래처,품목선택");
			lblNewLabel.setFont(new Font("돋움", Font.PLAIN, 12));
			lblNewLabel.setBounds(28, 71, 97, 23);
		}
		return lblNewLabel;
	}
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setBounds(10, 117, 507, 412);
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getScrollPane());
		}
		return panel;
	}
	
	private JPanel getPanel2() {
		if (panel2 == null) {
			panel2 = new JPanel();
			panel2.setBounds(10, 117, 507, 412);
			panel2.setLayout(new BorderLayout(0, 0));
			panel2.add(getScrollPane2());
		}
		return panel2;
	}
}
